package com.blue.http.config;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.List;
import java.util.Map;

/**
 * HttpConfig解析
 *
 * @author Jin Zheng
 * @date 2018-10-25
 */
public class HttpConfigParser extends AbstractSimpleBeanDefinitionParser
{
	public HttpConfigParser()
	{
	}

	@Override
	protected Class<?> getBeanClass(Element element)
	{
		return HttpConfig.class;
	}

	@Override
	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
	{
		try
		{
			NodeList pathList = element.getElementsByTagNameNS("http://www.blue.com/schema/http", "path-map");
			if (pathList.getLength() > 0)
			{
				Element node = (Element) pathList.item(0).getChildNodes().item(1);
				Map<Object, Object> map = parserContext.getDelegate().parseMapElement(node, builder.getRawBeanDefinition());
				builder.addPropertyValue("pathMap", map);
			}
			NodeList configList = element.getElementsByTagNameNS("http://www.blue.com/schema/http", "filter-configs");
			if (configList.getLength() > 0)
			{
				this.parseHttpFilterConfig(configList.item(0).getChildNodes(), builder.getRawBeanDefinition());
			}

			builder.addPropertyValue("path", element.getAttribute("path"));
			builder.addPropertyValue("ftlRoot", element.getAttribute("ftl-root"));
			builder.addPropertyValue("maxUploadSize", element.getAttribute("max-upload-size"));
		}
		catch (Exception e)
		{
			parserContext.getReaderContext().error(HttpConfig.class.getName() + " 无法创建", element, null, e);
		}
	}

	private void parseHttpFilterConfig(NodeList nodeList, BeanDefinition beanDefinition)
	{
		if (nodeList == null || nodeList.getLength() == 0)
			return;

		ManagedList<BeanDefinitionHolder> managedList = new ManagedList<>();
		for (int i = 0; i < nodeList.getLength(); i++)
		{
			Node node = nodeList.item(i);
			if (node instanceof Element)
			{
				Element element = (Element) node;
				RootBeanDefinition definition = new RootBeanDefinition();
				definition.setBeanClass(InvokerFilterConfig.class);
				definition.getPropertyValues().addPropertyValue("filters", element.getAttribute("filters"));
				definition.getPropertyValues().addPropertyValue("excludes", element.getAttribute("excludes"));
				definition.getPropertyValues().addPropertyValue("order", element.getAttribute("order"));
				String ref = element.getAttribute("ref");
				definition.getPropertyValues().addPropertyValue("object", new RuntimeBeanReference(ref));

				BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, InvokerFilterConfig.class.getName() + "-" + ref);
				managedList.add(holder);
			}
		}
		if (!managedList.isEmpty())
		{
			beanDefinition.getPropertyValues().addPropertyValue("filterConfigs", managedList);
		}
	}

}
